package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.List;
//https://www.bilibili.com/video/BV1jJ411S7xr?p=8&spm_id_from=pageDriver
@RestController
//@RequestMapping("/b")
public class DeptConsumerController{

/*    理解: 消费者  不应该有service请求
    springboot支持RestFul请求
    RestTemplate  模板 对应请IQUI 供我们直接调用就可以了 注册到spring中
    (URI url,实体:Map封装实体,Class<T> responseType)*/

    @Autowired
    private RestTemplate restTemplate; //提供多种便捷访问远程http服务的方法,简单的restful服务模板

    //请求地址写死因为是固定的
//    private static final String REST_URL_PREFIX = "http://localhost:8001/";
//    private static final String REST_URL_PREFIX = "http://localhost:8011/";

    //通过Ribbon  我们这里的地址应该是一个变量，通过服务名来访问
    private static final String REST_URL_PREFIX = "http://SPRING-CLOUD-PROVIDER-DEPT";

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Integer id) {
        System.out.println("80端口查询id");
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }


    @RequestMapping("/consumer/dept/get/list")
    public List<Dept> list() {
        System.out.println("80端口查询全部");
        return restTemplate.getForObject(REST_URL_PREFIX + "/queryAll", List.class);
    }


    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept) {
        System.out.println("80端口添加");
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }


    @RequestMapping(value = "/consumer/dept/discovery")
    public Object discovery() {
        Object forObject = restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
        System.out.println("运行到这了...");
        return forObject;
    }

    @RequestMapping("/cc")
    public String cc(){
        System.out.println("测试你好!");
        return "你好 ";
    }
}
